<template>
	<view>
		<view class="user">
			<view class="divHead">

				<u-avatar size="60" shape="circle" :src="user.avatarUrl"></u-avatar>

				<view class="divUserName" v-if="user">
					<u-text :text="user.nickName" bold="true"></u-text>
				</view>
				<view class="divLogin" v-else><button style="background: #cc6600;margin-left: 0;padding-left: 0"
						@click="login" size="mini">登录</button></view>
				<view class="userPhone">
					<u-text :text="phoneNumber"></u-text> 
				</view>
				<u-modal :show="show" :content='content'>
					<view style="font-size: 36rpx;text-align: center;">绑定手机号
						<view style="color: lightgray; font-size: 28rpx;text-align: center">请先绑定手机号再进行此操作</view>
					</view>
					<u-button openType="getPhoneNumber" @getphonenumber="confirm" type="success" slot="confirmButton"
						shape="circle" text="微信用户一键绑定"></u-button>
				</u-modal>
			</view>
			
			<scroll-view scroll-y="true" :style="{height: wh-75 + 'px'}">
				<view class="divContent">
					<view v-if="user" class="foot">
						<view @click="logout" class="logout">
							退出登录
						</view>
					</view>
				</view>
			</scroll-view>
			
		</view>
	</view>

</template>

<script>
	import {
		getBaseUrl,
		requestUtil,
		getWxLogin,
		getUserProfile,
	} from '../../utils/requestUtils.js';
	import regeneratorRuntime from '../../lib/runtime/runtime.js';
	export default {
		data() {
			return {
				content: "微信用户快速登录",
				phoneNumber: "",
				show: false,
				user: {},
				wh: 0,
			};
		},
		created() {
			
		},
		onShow() {
			
		},
		mounted() {
			const sysInfo = uni.getSystemInfoSync()
			this.wh = sysInfo.windowHeight
			this.getUserInfo()
		},
		methods: {
			
			confirm(e) {
				console.log(e.detail.errMsg)
				if (e.detail.errMsg == 'getPhoneNumber:ok') {

					let param = {
						code: e.detail.code
					}
					this.getPhoneNumber(param)
				} else {
					return uni.$showMsg('请授权登录!')
				}
			},  
			  
			async wxLogin(loginParam) {
				console.log("这里进入wxLogin方法")
				const res = await requestUtil({
					url: "/user/wxLogin",
					data: loginParam,
					method: "post"
				});

				console.log("判断调用成功后的res里code值：" + res.code)
				if (res.code === 1) {
					console.log("获取登录成功res.data.token的token："+res.data.token)
					wx.setStorageSync('token', res.data.token)
					if (res.data.phone.length == 0) {
						this.show = true
						uni.hideTabBar({
							animation: true
						})
					}
					wx.setStorageSync('phoneNumber', res.data.phone)
					this.getUserInfo()
					
				}
			},
			//登录最先启动的方法
			login() {
				Promise.all([getWxLogin(), getUserProfile()]).then((res) => {
					console.log("login登录成功返回值："+res)
					let loginParam = {
						code: res[0].code,
						nickName: res[1].userInfo.nickName,
						avatarUrl: res[1].userInfo.avatarUrl
					}
					console.log("login这里输出："+loginParam)
					this.wxLogin(loginParam);
				})
			},
			async getPhoneNumber(param) {
				console.log("getPhoneNumber方法")
				let that = this
				const res = await requestUtil({
					url: "/user/wxGetPhone",
					data: param,
					method: "post"
				})
				if (res.code === 1) {
					this.phone_info = res.data.phone_info
					console.log("手机号" + this.phone_info)
					let phoneNumber = this.phone_info.phoneNumber
					console.log(phoneNumber, "手机号")
					wx.setStorageSync('phoneNumber', phoneNumber)
					this.show = false
					this.getUserInfo()
					uni.showTabBar({
						animation: true
					})
					this.initData()

				} else {
					return uni.$showMsg()
				}
			},
			//获取用户登录信息，用于显示用户头像、名字和手机号
			getUserInfo() {
				this.user = wx.getStorageSync('userInfo')
				this.phoneNumber = wx.getStorageSync('phoneNumber')
				console.log('getUserInfo获取用户信息' + this.user)
			},
			
			logout() {
				wx.clearStorageSync()
				this.getUserInfo()
			}
		},

	}
</script>

<style>
	@import url(./my.css);
</style>
